package com.sensePlusVR.manager;

import java.util.ArrayList;
import java.util.List;

import com.sensePlusVR.bean.ActorItem;
import com.sensePlusVR.bean.BannersDto;
import com.sensePlusVR.bean.CollectFollowIds;
import com.sensePlusVR.bean.FeedbackListRespDto;
import com.sensePlusVR.bean.FeedbackReqDto;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.UserUtil;

public class DataCenter {
	private String TAG = DataCenter.class.getSimpleName();
	private String KEY = DataCenter.class.getSimpleName();
	private GameItemManager gameManager;
	private CollectionFollowManager CFManager;
	private ActorManager actorManager;
	private CommendItemManager commendManager;
	private FeedbackManager feedbackManager;
	private BannerManager bannerManager;
	
	private static DataCenter instans;
	
	public static DataCenter getInstans(){
		if (instans == null){
			instans = new DataCenter();
		}
		return instans;
	}
	
	private EventListener gameListener = new EventListener(){

		@SuppressWarnings("unchecked")
		@Override
		public void Notify(int event, Object data, Object Params) {
			// TODO Auto-generated method stub
			MyLog.e(TAG, "gameListener event="+event);
			switch(event){
			case GameItemManager.EventGameItemMoreGot:
			case GameItemManager.EventGameItemRefreshGot:
				boolean needPullActors = false;
				List<GameItem> newlist = (List<GameItem>)data;
				for (GameItem item:newlist){
					if (ActorItem.getActorByName(item.getActors()) == null){
						needPullActors = true;
						break;
					}
				}
				if (needPullActors){
					getActors(true);
				}
				break;
			}
		}
	};
	
	private EventListener listener = new EventListener(){

		@Override
		public void Notify(int event, Object data, Object Params) {
			// TODO Auto-generated method stub
			//
			MyLog.e(TAG, "dataCenter event="+event);
			switch(event){
			case CollectionFollowManager.EventFollowCollectionGot:
				CollectFollowIds ids = (CollectFollowIds)data;
				MyLog.d(TAG, "CollectionFollowManager.EventFollowCollectionGot");
				if (ids.getCollectionList().size() > 0){
					MyLog.e(TAG, "get Collection list");
					gameManager.getGameListByIds(ids.getCollectionList());
				}
				if (ids.getFollowList().size() > 0){
					MyLog.e(TAG, "get follow list");
					actorManager.getFollowActors(ids.getFollowList());
				}
				break;
			case CollectionFollowManager.EventCollectionAdded:
			case CollectionFollowManager.EventCollectionDeled:
				//getFollowedActors(userId);
			case CollectionFollowManager.EventFollowAdded:
			case CollectionFollowManager.EventFollowDeled:
				
				//getGameCollectons(userId);
				break;
			}
			if (event >= CollectionFollowManager.EventErrorBase) {
				event -= CollectionFollowManager.EventErrorBase;
				int resId = (Integer)data;
				switch(event){
				case CollectionFollowManager.EventCollectionAdded:
					GameItem gitem = GameItem.getGameById(resId);
					if(gitem != null){
						gitem.setColloctoned(0);
						GameItem.saveOrUpdate(gitem);
					}
				case CollectionFollowManager.EventCollectionDeled:
					 gitem = GameItem.getGameById(resId);
					if(gitem != null){
						gitem.setColloctoned(1);
						
						
						
						GameItem.saveOrUpdate(gitem);
					}
					break;
				case CollectionFollowManager.EventFollowAdded:
					ActorItem item = actorManager.getActorById(resId);
					if (item != null){
						item.setFollowed(0);
						item.setUserfollowcount(item.getUserfollowcount() - 1);
						actorManager.save(item);
					}
					break;
				case CollectionFollowManager.EventFollowDeled:
					item = actorManager.getActorById(resId);
					if (item != null){
						item.setFollowed(1);
						item.setUserfollowcount(item.getUserfollowcount() + 1);
						actorManager.save(item);
					}
					break;
				}
			}
		}
		
	};
	
	public DataCenter(){
		this.gameManager = GameItemManager.getInstants();
		this.CFManager = CollectionFollowManager.getInstants();
		this.actorManager = ActorManager.getInstants();
		this.commendManager = CommendItemManager.getInstants();
		this.feedbackManager = FeedbackManager.getInstants();
		this.bannerManager = BannerManager.getInstants();
		
		CFManager.addDataCenterEventListener(listener, this, KEY);
		gameManager.addDataCenterEventListener(gameListener, this, KEY);
		//predownload actors
		initDbCenter();
	}
	
	private void initDbCenter(){
		int userId = UserUtil.getUserId();
		//登陆过则才请求collection和actors列表(因为获取列表需要Token,DataCenter在application中初始化,造成第一次安装在over 18跳转登录界面问题)
		MyLog.d(TAG, "userId : " + userId);
		if(userId > 0){
			getActors(true);
			getGameCollectons(userId);
			//getGameListSort(true, true);
		}
	}
	
	public void retryCollections(){
		int userId = UserUtil.getUserId();
		
		CFManager.getFollowsAndCollections(userId, true);
	}
	
	public void addColFollowEventListener(EventListener l, Object p, String key){
		CFManager.addEventListener(l, p, key);
	}
	
	public void addGameEventListener(EventListener l, Object p, String key){
		gameManager.addEventListener(l, p, key);
	}
	
	public void addFCEventListener(EventListener l, Object p, String key){
		CFManager.addEventListener(l, p, key);
	}
	
	public void addActorEventListener(EventListener l, Object p, String key){
		actorManager.addEventListener(l, p, key);
	}
	
	public void addCommendEventListener(EventListener l, Object p, String key){
		commendManager.addEventListener(l, p, key);
	}
	
	public void addFeedbackEventListener(EventListener l, Object p, String key){
		feedbackManager.addEventListener(l, p, key);
	}
	
	public void addBannerEventListener(EventListener l, Object p, String key){
		bannerManager.addEventListener(l, p, key);
	}
	
	public void delGameEventListener(String key){
		gameManager.delEventListener(key);
	}
	
	public void delFCEventListener(String key){
		CFManager.delEventListener(key);
	}
	
	public void delActorEventListener(String key){
		actorManager.delEventListener(key);
	}
	
	public void delCommendEventListener(String key){
		commendManager.delEventListener(key);
	}
	
	public void delFeedbackEventListener(String key){
		feedbackManager.delEventListener(key);
	}
	
	public void delBannerEventListener(String key){
		bannerManager.delEventListener(key);
	}

	
	
	public void resetGameDb(){
		gameManager.resetDbIndex();
	}
	
	public List<GameItem> getGameListSort(boolean sort, boolean update,boolean isRefresh) {
		List<GameItem> list = gameManager.getGameListSort(sort, update,isRefresh);

		return list;
	}
	
	public List<GameItem> getGameListByActorName(String name, boolean update) {
		List<GameItem> list = gameManager.getGameListByActorName(name, update);
		return list;
	}
	
	public List<GameItem> getGameListByActorName(String name,boolean update,boolean desc){
		List<GameItem> list = gameManager.getGameListByActorName(name, update,desc);
		return list;
	}
	
//	public GameItem getGameById(int gameId){
//		List<Integer> ids = new ArrayList<Integer>();
//		ids.add(gameId);
//		List<GameItem> list = gameManager.getGameListByIds(ids);
//		if (list != null && list.size() > 0){
//			return list.get(0);
//		}
//		return null;
//	}
	
	public List<GameItem> getGameCollectons(int userId){
		CollectFollowIds ids =  CFManager.getFollowsAndCollections(userId, false);
		if (ids != null){
			
			ArrayList<Integer> arrInt = ids.getCollectionList();
			
			if(arrInt!=null&&arrInt.size()>0){
				
				return gameManager.getGameListByIds(ids.getCollectionList());
			}
			
		}
		return null;
	}
	
	/**
	 * 根据登录信息获取ids
	 * @param userId
	 * @return
	 */
	public CollectFollowIds getCollectFollowIds(int userId){
		
		CollectFollowIds ids =  CFManager.getFollowsAndCollections(userId, false);
		
		return ids;
	}
	
	public GameItem getGameCommend(int gameId){
		return commendManager.getCommend(gameId,false);
	}
	
	public GameItem getGameCommend(int gameId,boolean update){
		return commendManager.getCommend(gameId,update);
	}
	
	public void setGameCommend(int gameId){
		commendManager.setCommend(gameId);
	}
	
	public void FollowActorAdd(int resId){
		int userId = UserUtil.getUserId();
		ActorItem item = actorManager.getActorById(resId);
		if (item != null){
			item.setFollowed(1);
			item.setUserfollowcount(item.getUserfollowcount() + 1);
			actorManager.save(item);
		}
		CFManager.FollowAdd(userId, resId);
	}
	
	public void FollowActorDel(int resId){
		int userId = UserUtil.getUserId();
		ActorItem item = actorManager.getActorById(resId);
		if (item != null){
			item.setFollowed(0);
			item.setUserfollowcount(item.getUserfollowcount() - 1);
			actorManager.save(item);
		}
		CFManager.FollowDel(userId, resId);
	}
	
	public void CollectionGameAdd(int resId){ 
		int userId = UserUtil.getUserId();
		CFManager.CollectionAdd(userId, resId);
	}
	
	public void CollectionGameDel(int resId){
		int userId = UserUtil.getUserId();
		CFManager.CollectionDel(userId, resId);
	}
	
	public void CollectionedDownload(int resId){
		int userId = UserUtil.getUserId();
		CFManager.CollectionedDownload(userId, resId);
		
	}
	
	public ActorItem getActorById(int actorId){
		return actorManager.getActorById(actorId);
	}
	
	public ActorItem getActorByName(String name){
		return actorManager.getActorByName(name);
	}
	
	public List<ActorItem> getActors(boolean force){
		return actorManager.getAllActors(force);
	}
	
	public List<ActorItem> getFollowedActors(int userId){
		if (userId == -1)
			userId = UserUtil.getUserId();
		CollectFollowIds ids =  CFManager.getFollowsAndCollections(userId, false);
		if (ids != null){
			
			MyLog.d(TAG, "ids : " + ids.toString());
			
			ArrayList<Integer> arrInt = ids.getFollowList();
			
			if(arrInt!=null&&arrInt.size()>0){
				
				MyLog.d(TAG, "arrInt : " + arrInt.toString());
				
				return actorManager.getFollowActors(arrInt);
			}
			
		
		}
		return null;
	}
	
	public Integer getFeedbackNoReadCount(){
		return feedbackManager.getFeedbackNoReadCount();
	}
	
	public List<FeedbackListRespDto> getFeedbackList(FeedbackReqDto feedbackReqDto){
		return feedbackManager.getFeedbackList(feedbackReqDto);
	}
	
	public List<BannersDto> getBannerList(int commonType){
		return getBannerList(commonType,false);
	}
	
	public List<BannersDto> getBannerList(int commonType,boolean update){
		return bannerManager.getBannerList(commonType,update);
	}
	
	
	
}
